home *** CD-ROM | disk | FTP | other *** search
- Path: hubcap.clemson.edu!hubcap!mjs
- From: mjs@hubcap.clemson.edu (M. J. Saltzman)
- Newsgroups: comp.lang.c
- Subject: Re: c++ and readability
- Date: 13 Feb 96 23:55:06 GMT
- Organization: Clemson University
- Message-ID: <mjs.824255706@hubcap>
- References: <DMoyuG.CD4@world.std.com> <312097FF.41C67EA6@nada.kth.se> <4fqqrv$np4@solutions.solon.com>
- NNTP-Posting-Host: hubcap.clemson.edu
-
- seebs@solutions.solon.com (Peter Seebach) writes:
-
- >In article <312097FF.41C67EA6@nada.kth.se>,
- >Fredrik Rubensson <prgp-fru@nada.kth.se> wrote:
- >>1. Press return before and after {.
- >>2. Insert empty lines between code blocks doing different things.
- >>(3. Of course good comments increase the readability very much.)
-
- >These are both unnecessary and insufficient. 1TBS (The One True Brace Style
- >used in K&R, both editions) is an excellent and readable style.
-
- >You missed the *crucial* item:
- >1. Indentation should reflect structure.
-
- Along these lines, a useful suggestion is: If a comment refers to a
- group of lines, indent the group of lines (by enclosing in a block).
-
- Thus, for example:
-
- /* Scan the linked list to see if new_key is already on it.
- If not, insert new node at end of list. */
- {
- list_node *s = NULL, *t = list_head;
-
- while ( t != NULL && t->key != new_key ) {
- s = t;
- t = t->next;
- }
-
- if ( s == NULL ) {
- list_head = new_node(new_key);
- list_head->next = NULL;
- } else {
- t = new_node(new_key);
- t->next = s->next->next;
- s->next = t;
- }
- }
-
- Otherwise, there's no really good way to mark the end of the code
- being referred to by the comment, especially if you have commented
- subblocks within commented blocks. Also, you can declare variables
- needed only for the block inside the block.
-
- >Also important:
- >2. Declare functions liberally. Never write the same code twice.
-
- If code should be set off, but is not reused, this technique is often
- enough, and you don't have to create long parameter lists. If you do
- discover a need to reuse the code, a block witten this way is almost
- ready to be moved directly into a function.
-
- >[...]
-
- --
- Matthew Saltzman
- Clemson University Math Sciences
- mjs@clemson.edu
-